package com.sgb.kjwl.view.ui.adapter.pick.holder;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.callback.OnItemClickListener;
import com.sgb.kjwl.model.entity.pick.EnterpriseBiddingDetailEntity5;
import com.sgb.kjwl.view.anim.ExpandableViewHoldersUtil;
import com.sgb.kjwl.view.ui.adapter.pick.PickEnterpriseMultiItemAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.ImageAdapter;
import com.swgk.core.base.CallBack;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PickEnterpriseMultiltemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, ExpandableViewHoldersUtil.Expandable {

    public ExpandableViewHoldersUtil.KeepOneHolder<PickEnterpriseMultiltemViewHolder> keepOne = new ExpandableViewHoldersUtil.KeepOneHolder<>();
    private PickEnterpriseMultiltemViewHolder mHolder;
    private Context mContext;

    public CheckBox ivSelect;
    private TextView titleType;
    private LinearLayout llIsOpen;
    private TextView tvTypeFillIn;
    public ImageView ivTypeOpen;

    private View mBottomLayout;
    private TextView surveyView;
    private TextView expandView;
    private TextView tvNum;
    private EditText etAskDescribe;
    private RecyclerView recyclerView;
    private List<File> fileList;
    private List<String> urls;
    private ImageAdapter imageAdapter;
    private OnItemClickListener<EnterpriseBiddingDetailEntity5.TenderingAsksEntity> onItemClickListener;
    private CallBack callBack;
    public int pos = -1;

    public PickEnterpriseMultiltemViewHolder(Context mContext, @NonNull View itemView, CallBack callBack, OnItemClickListener<EnterpriseBiddingDetailEntity5.TenderingAsksEntity> onItemClickListener) {
        super(itemView);
        mHolder = this;
        this.mContext = mContext;
        this.callBack = callBack;
        this.onItemClickListener = onItemClickListener;
        ivSelect = itemView.findViewById(R.id.iv_select);
        //取得设置好的drawable对象
        Drawable drawable = mContext.getResources().getDrawable(R.drawable.check_bg_select);
        //设置drawable对象的大小
        drawable.setBounds(0, 0, 60, 60);

        //设置CheckBox对象的位置，对应为左、上、右、下
        ivSelect.setCompoundDrawables(drawable, null, null, null);

        titleType = itemView.findViewById(R.id.title_type);
        llIsOpen = itemView.findViewById(R.id.ll_is_open);
        tvTypeFillIn = itemView.findViewById(R.id.tv_type_fill_in);
        ivTypeOpen = itemView.findViewById(R.id.iv_type_open);

        surveyView = itemView.findViewById(R.id.survey_view);
        expandView = itemView.findViewById(R.id.expand_view);
        etAskDescribe = itemView.findViewById(R.id.et_ask_describe);
        tvNum = itemView.findViewById(R.id.tv_num);
        recyclerView = itemView.findViewById(R.id.recycler_view);

        expandView.setOnClickListener(this);
        mBottomLayout = itemView.findViewById(R.id.layout_bottom_item);
    }

    //绑定数据
    public void bind(int pos, EnterpriseBiddingDetailEntity5.TenderingAsksEntity tenderingAsksEntity, PickEnterpriseMultiItemAdapter pickEnterpriseMultiItemAdapter) {
        keepOne.bind(this, pos);
        setTopViewData(pos, tenderingAsksEntity);
        setBottomData(pos, tenderingAsksEntity);
    }

    private void setTopViewData(int pos, EnterpriseBiddingDetailEntity5.TenderingAsksEntity tenderingAsksEntity) {
        ivSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tenderingAsksEntity.setIsResponse(2);
                } else {
                    tenderingAsksEntity.setIsResponse(1);
                }
                if (callBack != null) {
                    callBack.onSuccess(0, "选中", 0, null, "");
                }
            }
        });

        if (tenderingAsksEntity.getIsResponse() == 2) { //1:未响应 2：响应
            ivSelect.setChecked(true);
        } else {
            ivSelect.setChecked(false);
        }
        titleType.setText(tenderingAsksEntity.getTitleType());
        llIsOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setOpenView(pos, tenderingAsksEntity);
                if (callBack != null) {
                    callBack.onSuccess(0, "展开", 0, null, "");
                }
            }
        });

    }

    private void setBottomData(int pos, EnterpriseBiddingDetailEntity5.TenderingAsksEntity tenderingAsksEntity) {
        mHolder.surveyView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                surveyView.getViewTreeObserver().removeOnPreDrawListener(this);
                int line = surveyView.getLineCount();
                if (line <= 3) {
                    mHolder.expandView.setVisibility(View.GONE);
                }
                return false;
            }
        });
        mHolder.surveyView.setText(tenderingAsksEntity.getContentText());
        mHolder.etAskDescribe.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = TextUtils.isEmpty(s.toString().trim()) ? "" : s.toString().trim();
                if (!TextUtils.isEmpty(text)){
                    tvNum.setText(text.length() + "/200");
                }
                tenderingAsksEntity.setAskDescribe(text);
                if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(mHolder.surveyView.getText())) {
                    tvTypeFillIn.setText("已填写");
                    tvTypeFillIn.setTextColor(mContext.getResources().getColor(R.color._4d000000));
                } else {
                    tvTypeFillIn.setText("未填写");
                    tvTypeFillIn.setTextColor(mContext.getResources().getColor(R.color.black));
                }
            }
        });
        mHolder.etAskDescribe.setText(tenderingAsksEntity.getAskDescribe());
        imageAdapter = new ImageAdapter(mContext, null, null, 1, new CallBack() {
            @Override
            public void onSuccess(int i, String s, int i1, Object o, String s1) {
            }

            @Override
            public void onFailed(int i, int i1, String s) {
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(mHolder.mBottomLayout, pos, tenderingAsksEntity);
                }
            }
        });
        mHolder.recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        mHolder.recyclerView.setAdapter(imageAdapter);
        if (!TextUtils.isEmpty(tenderingAsksEntity.getFileUrl())) {
            List<String> netImgList = new ArrayList<>();
            netImgList.add(tenderingAsksEntity.getFileUrl());
            imageAdapter.setNetImgList(netImgList);
            imageAdapter.notifyDataSetChanged();
        }
    }

    private void setOpenView(int pos, EnterpriseBiddingDetailEntity5.TenderingAsksEntity tenderingAsksEntity) {
        //-1表示所有item是关闭状态，opend为pos值的表示pos位置的item为展开的状态
        if (tenderingAsksEntity.isExpandable()) {
            this.pos = -1;
            tenderingAsksEntity.setExpandable(false);
        } else {
            this.pos = getAdapterPosition();
            tenderingAsksEntity.setExpandable(true);
        }
        keepOne.toggle(mHolder, ivTypeOpen, 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.expand_view:
                if (expandView.getText().equals("展开")) {
                    expandView.setMaxLines(Integer.MAX_VALUE);
                    expandView.setText("收起");
                } else {
                    surveyView.setMaxLines(3);
                    expandView.setText("展开");
                }
                break;
        }
    }

    @Override
    public View getExpandView() {
        return mBottomLayout;
    }

}
